﻿using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonClassLibrary
{
    internal static class JsonHelper
    {
        public static string ToJson(this object obj)
        {
            IsoDateTimeConverter dateFormat = new IsoDateTimeConverter();
            dateFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";//设置事件的格式
            return JsonConvert.SerializeObject(obj, dateFormat);
        }
        public static T ToObject<T>(this string jsonStr) where T : new()
        {
            if (string.IsNullOrWhiteSpace(jsonStr))
                return new T();
            return JsonConvert.DeserializeObject<T>(jsonStr);
        }
    }
}
